package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.dao.event.EventInfoDao;
import cn.topevery.garden.dal.dao.impl.InteractPlatformDaoImpl;
import cn.topevery.garden.dal.logic.GreTreeFamousLogic;
import cn.topevery.garden.dal.logic.InteractPlatformFileLogic;
import cn.topevery.garden.dal.logic.InteractPlatformLogic;
import cn.topevery.garden.dal.logic.event.EventInfoLogic;
import cn.topevery.garden.dal.util.QueryWrapper;
import cn.topevery.garden.entity.dto.InteractPlatformDTO;
import cn.topevery.garden.entity.dto.event.EventInfoDTO;
import cn.topevery.garden.entity.po.GreTreeFamous;
import cn.topevery.garden.entity.po.InteractPlatform;
import cn.topevery.garden.entity.po.InteractPlatformFile;
import cn.topevery.garden.entity.po.event.EventInfo;
import cn.topevery.garden.entity.query.InteractPlatformQuery;
import cn.topevery.garden.entity.vo.InteractPlatformVO;
import cn.topevery.garden.entity.vo.event.ActivityInstanceVO;
import com.github.dozermapper.core.Mapper;
import com.github.dozermapper.core.MappingException;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 互动平台
 *
 * @author code-generate
 * @date 2022-02-18
 */
@Api(tags = "互动平台")
@RestController
@RequestMapping("/interactPlatform")
public class InteractPlatformController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    InteractPlatformLogic interactPlatformLogic;

    @Autowired
    InteractPlatformDaoImpl interactPlatformDao;

    @Autowired
    InteractPlatformFileLogic interactPlatformFileLogic;

    @Autowired
    EventInfoLogic eventInfoLogic;

    @Autowired
    GreTreeFamousLogic greTreeFamousLogic;

    @Autowired
    private EventInfoDao eventInfoDao;

    @ApiOperation("获取案件流程")
    @GetMapping("/getFlowsByEvtId")
    public JsonResult<List<ActivityInstanceVO>> getFlowsByEvtId(String evtId) {
        JsonResult<List<ActivityInstanceVO>> baseResult = new JsonResult<>();
        try {
            EventInfo gardenEvent = eventInfoDao.getEntityByColumn("c_evt_id", evtId);
            baseResult.data = eventInfoLogic.getFlowsByIds(gardenEvent.getWfInstId(), evtId);
        } catch (Exception ex) {
            baseResult.success = false;
            baseResult.msg = ex.getMessage();
        }
        return baseResult;
    }

    @ApiOperation("问题上报")
    @PostMapping("/saveWeChatEvt")
    public JsonResult<Boolean> saveWeChatEvt(@RequestBody EventInfoDTO evt) {

        SysUser u = getUser();
        eventInfoLogic.saveEvent(u, evt);
        if (!evt.getDtoList().isEmpty()) {
            for (InteractPlatformDTO dto : evt.getDtoList()) {
                interactPlatformDao.updateByTable(getUser().id.toString(),
                        new QueryWrapper<>().eq("c_status", 2).eq("c_evt_id", evt.getEvtId()),
                        new QueryWrapper<>().eq("c_id", dto.getId()));
            }
        }
        return JsonResult.ok(true);
    }

    /**
     * 获取互动平台分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取互动平台页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<InteractPlatformVO>> listPage(@RequestBody InteractPlatformQuery query) {
        DataPager<InteractPlatformVO> page = interactPlatformLogic.listPage(query);
        for (InteractPlatformVO interactPlatformVO : page.records) {
            List<InteractPlatformFile> list = interactPlatformFileLogic.getListByMainId(interactPlatformVO.getId());
            interactPlatformVO.setFileList(list);
        }
        return JsonResult.ok(page);
    }

    @ApiOperation(value = "查阅")
    @PostMapping("/console")
    public JsonResult<?> console(@RequestBody List<InteractPlatformDTO> dtoList) {
        for (InteractPlatformDTO dto : dtoList) {
            interactPlatformDao.updateByTable(getUser().id.toString(),
                    new QueryWrapper<>().eq("c_status", 1),
                    new QueryWrapper<>().eq("c_id", dto.getId()));
        }
        return JsonResult.ok(true);
    }

    @ApiOperation("新增/编辑 互动平台信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody InteractPlatformDTO dto) {
        InteractPlatform interactPlatform = mapper.map(dto, InteractPlatform.class);
        GreTreeFamous greTreeFamous = greTreeFamousLogic.getById(dto.getTreeId());
        interactPlatform.setStatus(0).setTreeName(greTreeFamous.getName()).setTreeCode(greTreeFamous.getCode()).setLat(greTreeFamous.lat).setLng(greTreeFamous.lng).setAddress(greTreeFamous.address);
        if (interactPlatform.getId() == null) {
            interactPlatform.setDbStatus(false);
        }
        String userId = getUser().id.toString();
        try {
            interactPlatformLogic.saveOrUpdate(userId, interactPlatform);
            interactPlatformFileLogic.saveOrUpdate(userId, interactPlatform.getId(), interactPlatform.getFileList());
            return JsonResult.ok(true);
        } catch (MappingException e) {
            e.printStackTrace();
            return JsonResult.ok(false);
        }
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<InteractPlatformVO> getInfoById(@RequestParam Integer id) {
        InteractPlatform entity = interactPlatformLogic.getEntity(id);
        InteractPlatformVO vo = mapper.map(entity, InteractPlatformVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        interactPlatformLogic.deleteBatchById(getUser().id.toString(), ids);
        interactPlatformFileLogic.deleteByMainIds(ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出互动平台信息")
    @GetMapping("/export")
    public void export(InteractPlatformQuery query, HttpServletResponse response) {
        List<InteractPlatformVO> list = interactPlatformLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "互动平台", "sheet1", InteractPlatformVO.class, "互动平台", response);
    }

}
